<template>
    <div class="search-box">
        <div class="row">
            <div class="left">
                <el-icon>
                    <Search />
                </el-icon>
                <span>筛选搜索</span>
            </div>
            <div class="right">
                <el-button @click="queryReset">重置</el-button>
                <el-button type="primary" @click="searchHandle">查询搜索</el-button>
            </div>
        </div>
        <div class="row">
            <div>
                <span>输入搜索:</span>
                <el-input style="max-width: 223px;min-width: 200px;" placeholder="订单编号"
                    v-model="form.orderSn"></el-input>
            </div>
            <div>
                <span>收货人:</span>
                <el-input style="max-width: 223px;min-width: 200px;" placeholder="收货人姓名/手机号码"
                    v-model="form.receiverKeyword"></el-input>
            </div>
            <div>
                <span>提交时间:</span>
                <el-date-picker v-model="form.createTime" type="date" placeholder="请选择日期" size="default"
                    @change="changeTime('createTime')" />
            </div>
            <div>
                <span>订单状态:</span>
                <el-cascader placeholder="全部" v-model="status" :options="options" />
            </div>
            <div>
                <span>订单分类:</span>
                <el-cascader placeholder="全部" v-model="orderType" :options="options1" />
            </div>
            <div>
                <span>订单来源:</span>
                <el-cascader placeholder="全部" v-model="sourceType" :options="options2" />
            </div>
        </div>
    </div>
    <div class="search-box">
        <div class="row_tow">
            <div class="left">
                <el-icon>
                    <Tickets />
                </el-icon>
                <span>数据列表</span>
            </div>
            <!-- <div class="right">
                <el-button type="primary" @click="addUser()" v-permission="'addUser'">添加</el-button>
            </div> -->
        </div>
    </div>
    <el-table :data="list" border table-layout="auto" cell-class-name="align-c"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="60" />
        <el-table-column label="编号" prop="id" />
        <el-table-column label="订单编号" prop="orderSn" width="200" />
        <el-table-column label="提交时间" width="200">
            <template #default="{ row }">
                <span>{{ formateDate(row.createTime) }}</span>
            </template>
        </el-table-column>
        <el-table-column label="用户账号" prop="memberUsername" />
        <el-table-column label="订单金额" prop="totalAmount" />
        <el-table-column label="支付方式">
            <template #default="{ row }">
                <span>{{
                    row.payType == 0 ? '未支付' : (row.payType == 1 ? '支付宝' : (row.payType == 2 ? '微信' : ''))
                }}</span>
            </template>
        </el-table-column>
        <el-table-column label="订单来源">
            <template #default="{ row }">
                <span>{{ row.sourceType == 1 ? 'APP订单' : 'PC订单' }}</span>
            </template>
        </el-table-column>
        <el-table-column label="订单状态">
            <template #default="{ row }">
                <span>{{
                    row.status == 0 ? '待付款' : (row.status == 1 ? '待发货' : (row.status == 2 ? '已发货' : (row.status == 3 ?
                        '已完成' : (row.status == 4 ? '已关闭' : ''))))
                }}</span>
            </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="230">
            <template #default="{ row }">
                <el-button @click="router.push(`/oms/orderDetail?id=${row.id}`)">查看订单</el-button>
                <el-button type="danger" v-if="row.status === 4" @click="deleUser(row.id)">删除订单</el-button>
                <el-button v-if="row.status !== 4">订单跟踪</el-button>
            </template>
        </el-table-column>
    </el-table>
    <div class="left">
        <el-cascader placeholder="全部" v-model="DeliveryType" :options="Delivery" />
        <el-button type="primary" style="margin-left: 10px;" @click="ConfirmTn">确定</el-button>
    </div>
    <el-config-provider :locale="zhCn">
        <div class="page">
            <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[10, 20, 50, 100]"
                background layout="total, sizes, prev, pager, next, jumper" :total="total"
                @size-change="pageChangeHandle" @current-change="pageSizeChangeHandle" />
        </div>
    </el-config-provider>
    <orderRemarks :isShow="showDialog" @hide="closeDialog" :deleteList="deleteList" />
</template>
<script setup lang='ts'>
import useTable from "@/hooks/useTable";
import { getOrderDeleteApi, getOrderDeleteTwoApi, getOrderListApi } from "./api"
import { formateDate } from "@/utils/index";
import { ElConfigProvider, ElMessage, ElMessageBox } from 'element-plus'
import zhCn from 'element-plus/lib/locale/lang/zh-cn'
import { reactive, ref } from "vue";
import { Search, Tickets } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import orderRemarks from "./components/orderRemarks.vue"

const router = useRouter()

const status = ref([])
const orderType = ref([])
const sourceType = ref([])
const DeliveryType = ref([])
const deleteList = ref<number[]>([])
const showDialog = ref(false)

interface User {
    id: number,
    orderSn: string,
    createTime: string,
    memberUsername: string,
    totalAmount: number,
    payType: number,
    sourceType: number,
    status: number
}
// const multipleSelection = ref<User[]>([])
const handleSelectionChange = (val: User[]) => {
    val.forEach(item => {
        list.value.find((sur: { id: number; }) => sur.id === item.id)
        deleteList.value.push(item.id)
    })
}


const options = [
    { value: '0', label: '待付款', },
    { value: '1', label: '待发货', },
    { value: '2', label: '已发货', },
    { value: '3', label: '已完成', },
    { value: '4', label: '已关闭', },
    { value: '5', label: '无效订单', },
]
const options1 = [
    { value: '0', label: '正常订单', },
    { value: '1', label: '秒杀订单', },
]
const options2 = [
    { value: '0', label: 'PC订单', },
    { value: '1', label: 'app订单', },
]

const Delivery = [
    { value: '0', label: '批量发货', },
    { value: '1', label: '关闭订单', },
    { value: '2', label: '删除订单', },
]

const changeTime = (time: string) => {
    if (time == 'createTime') form.createTime = formateDate(form.createTime).slice(0, 10)
}

const form = reactive({
    orderSn: '',
    receiverKeyword: '',
    createTime: ''
})

const searchHandle = () => {
    getOrderListApi(
        {
            pageNum: 1,
            pageSize: 10,
            status: status.value[0],
            orderType: orderType.value[0],
            sourceType: sourceType.value[0],
            orderSn: form.orderSn,
            receiverKeyword: form.receiverKeyword,
            createTime: form.createTime
        }
    ).then(res => {
        if (res.code === 200) {
            // if()
            list.value = res.data.list
            total.value = res.data.total
        }
    })
}
const queryReset = () => {
    status.value = []
    orderType.value = []
    sourceType.value = []
    form.orderSn = ''
    form.receiverKeyword = ''
    form.createTime = ''
    getOrderListApi(
        {
            pageNum: 1,
            pageSize: 10,
        }
    ).then(res => {
        if (res.code === 200) {
            // if()
            list.value = res.data.list
            total.value = res.data.total
        }
    })
}


// 删除订单
const deleUser = (id: number) => {
    ElMessageBox.confirm("是否要进行该删除操作?", "提示?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: 'warning',
    }).then(() => {
        getOrderDeleteApi(id).then(res => {
            if (res.code === 200) {
                ElMessage.success('删除成功')
                searchHandle()
            }
        })
    })
}

// 子传父触发
const closeDialog = (val: boolean) => {
    showDialog.value = false
    if (val) searchHandle()
}

// 确定
const ConfirmTn = () => {
    if (deleteList.value.length == 0) {
        ElMessage.warning('请选择要操作的订单')
        return
    }
    if (DeliveryType.value[0] == 2) {
        getOrderDeleteTwoApi(deleteList.value).then(res => {
            if (res.code === 200) {
                ElMessage.success('删除成功')
                searchHandle()
            }
        })
    } else if (DeliveryType.value[0] == 1) {
        showDialog.value = true
    }
}


const {
    list,
    pageNum,
    pageSize,
    total,
    pageChangeHandle,
    pageSizeChangeHandle,
} = useTable(getOrderListApi, form);
</script>

<style lang="less" scoped>
:deep(.cell) {
    text-align: center;

}

:deep(.el-table__cell) {
    text-align: center;
    height: 53px;
}

.search-box {
    box-shadow: 0 0 4px 2px #ccc;
    border-radius: 5px;
    padding: 10px;
    margin-bottom: 20px;

    .row {
        padding: 0 0 20px 0;
        display: flex;
        align-items: center;

        &:first-child {
            justify-content: space-between;
        }

        &:last-child {
            margin-left: 50px;
            display: flex;
            flex-wrap: wrap;
            // justify-content: space-around;

            div {
                display: flex;
                width: 400px;
                margin-bottom: 10px;

                span {
                    width: 100px;
                }
            }
        }
    }

    .left {
        margin-left: 10px;

        span {
            margin-left: 10px;
        }

    }

    .row_tow {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
}

.left {
    margin: 20px 0;
    height: 20px;
    float: left;
}

.page {
    margin: 20px 0;
    height: 20px;

    :deep(.el-pagination) {
        float: right;

    }
}
</style>